package com.test.filter;

import com.test.util.Constant;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/*
        if(request.getSession().getAttribute(Constant.USER_SESSION.level) == VIP1){
            response.sendRedirect("/vip1/index.jsp");
        }
        if(request.getSession().getAttribute(Constant.USER_SESSION.level) == VIP2){
            response.sendRedirect("/vip2/index.jsp");
        }
 */


public class SysFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        Filter.super.init(filterConfig);
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {

        //将 servletRequest 强制转换成 HttpServletRequest, 从而获取session
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;

        if(request.getSession().getAttribute(Constant.USER_SESSION) == null){
            //response.sendRedirect(request.getContextPath()+"/error.jsp");
            response.sendRedirect("/LoginSystem/error.jsp");
        }

       filterChain.doFilter(request,response);

    }

    @Override
    public void destroy() {
        Filter.super.destroy();
    }
}
